#ifndef __PLATFORM_H__
#define __PLATFORM_H__

#include "FreeRTOS.h"
#include "task.h"
#include <semphr.h>
#include "typedef.h"
#include "delay.h"

/*任务等级*/
#define OS_TASK_PRIO_IDLE           ((uint32_t)configUSE_TICKLESS_IDLE)
#define OS_TASK_PRIO_LOW            ((uint32_t)1)
#define OS_TASK_PRIO_BELOW_NORMAL   ((uint32_t)OS_TASK_PRIO_LOW+2)
#define OS_TASK_PRIO_NOMAL          ((uint32_t)OS_TASK_PRIO_LOW+3)
#define OS_TASK_PRIO_ABOVE_NOMAL    ((uint32_t)OS_TASK_PRIO_LOW+4)
#define OS_TASK_PRIO_HIGHT          ((uint32_t)OS_TASK_PRIO_LOW+5)
#define OS_TASK_PRIO_REALTIME       ((uint32_t)OS_TASK_PRIO_LOW+6)

/*任务独占空间*/
#define OS_STACK_SIZE_LITTLE      ((uint32_t)configMINIMAL_STACK_SIZE)
#define OS_STACK_SIZE_MEDIUM      ((uint32_t)configMINIMAL_STACK_SIZE * 2)
#define OS_STACK_SIZE_LARGE       ((uint32_t)configMINIMAL_STACK_SIZE * 4)
#define OS_STACK_SIZE_SUPER_LARGE ((uint32_t)configMINIMAL_STACK_SIZE * 6)

#define BIT(n) ((unsigned int)1<<(n))
#define ISBIT(s,n) ((s) & BIT(n))

#define OS_ASSERT(message, assertion) do { if (!(assertion)) { \
  log(LOG_ERR, message); assert(assertion); }} while(0)

#define os_pthread_t TaskHandle_t
#define os_pthread_create(name, pthread, func, para, prio, size) pal_pthread_create(name, pthread, func, para, prio, size)
#define os_pthread_destroy(pthread) vTaskDelete(pthread) 
//互斥锁
#define os_mutex_t SemaphoreHandle_t
#define os_mutex_create() (xSemaphoreCreateMutex())
#define os_mutex_lock(mtx_ptr) pal_mutex_lock(*(mtx_ptr))
#define os_mutex_lock_wait(mtx_prt, tick) pal_mutex_lock_wait((*mtx_prt), (tick))
#define os_mutex_unlock(mtx_ptr) pal_mutex_unlock(*mtx_ptr)
#define os_mutex_destroy(mtx_ptr) vSemaphoreDelete(*mtx_ptr)

//信号量
#define os_sem_t SemaphoreHandle_t
#define os_sem_create(sem,num) (sem = xSemaphoreCreateBinary())
#define os_sem_pend(sem, t) xSemaphoreTake(sem,t)
#define os_sem_post(sem) xSemaphoreGive(sem)
#define os_sem_destroy(sem) vSemaphoreDelete(sem)

//队列  
#define os_queue_t xQueueHandle
#define os_queue_create(num, size) xQueueCreate(num, size)
#define os_queue_send(h, item) xQueueSend(h, item, 10)
#define os_queue_send_wait(h,item, t) xQueueSend(h, item, t)
#define os_queue_read(h, item) xQueueReceive(h, item, 10)
#define os_queue_read_wait(h, item, t) xQueueReceive(h, item, t)
#define os_queue_destroy(h) vQueueDelete(h)

#define os_msleep(ms) delay_ms(ms)
#define os_reboot() Reboot()

//周期性运行的任务不可申请堆内存
#define os_malloc(size) pvPortMalloc(size)
#define os_free(p) vPortFree(p)

typedef uint64_t mqtt_pal_time_t;
typedef SemaphoreHandle_t mqtt_pal_mutex_t;

int32_t pal_pthread_create(char_t* threadName ,os_pthread_t *thread ,void (*start_routine)(void *arg) ,void *para ,int32_t prio ,int32_t stack_size ) ;
int32_t pal_mutex_lock(os_mutex_t mutex);
int32_t pal_mutex_lock_wait(os_mutex_t mutex, int32_t tick);
int32_t pal_mutex_unlock(os_mutex_t mutex);

typedef int32_t pal_socket_t;
struct SocketApi{
    pal_socket_t (*create)(const char_t *addr, int32_t port);
    int32_t (*send)(pal_socket_t s, const void* buf, size_t len, int flags);
    int32_t (*recv)(pal_socket_t s, void* buf, size_t bufsz, int flags);
    void (*close)(pal_socket_t s);
};
/*mqtt 调用网络 接口*/
int32_t net_init(char_t *ip, uint32_t port);
struct SocketApi *net_get_socket_api(void);

#endif
